<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Area;
use App\Address;

class AddressController extends Controller
{
    //
    public function area(Request $request)
    {

        $areas = Area::where('parentid', $request->pid)->orderBy('areaid','asc')->get();
// dd($areas);
        return response()->json($areas);
    }

    public function insert(Request $request)
    {
        // dd($request->all());
        //实例化模型对象
        $address = new Address;
        $address -> name = $request->name;
        $address -> phone = $request->phone;
        $address -> sheng = $request->sheng;
        $address -> shi = $request->shi;
        $address -> xian = $request->xian;
        $address -> detail = $request->detail;
        $address -> user_id = session('id');
        $address -> is_default = $request->input('isdefault', 1);

        // dd($address);
        if($address->save()) {
            return back()->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }
}
